// pages/my/print/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        codes: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let userInfo = wx.getStorageSync('userInfo');
        if(userInfo){
            this.getUserInfo();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //获取用户信息
    getUserInfo: function() {
        wx.$http({
            load: true,
            url: '/business/get-info',
            method: 'get',
            data: {}
        }).then((res) => {
            if(res.code == 1){
                let data = res.data;
                let device = data.device;
                device.forEach(item=>{
                    item.status = 1;
                })
                this.setData({
                    codes: device
                })
            }
        })
    },
    //设置key
    bindKey: function(e) {
        let {codes} = this.data;
        let v = e.detail.trim();
        let index = e.currentTarget.dataset.index;
        codes[index]['key'] = v;
        this.setData({
            codes: codes
        })
    },
    //设置sn
    bindSn: function(e) {
        let {codes} = this.data;
        let v = e.detail.trim();
        let index = e.currentTarget.dataset.index;
        codes[index]['sn'] = v;
        this.setData({
            codes: codes
        })
    },
    //添加设备
    add: function() {
        let {codes} = this.data;
        codes.push({key: '',sn: '',status:0});
        this.setData({
            codes: codes
        })
    },
    //移除设备
    remove: function(e) {
        let {codes} = this.data;
        let index = e.currentTarget.dataset.index;
        let _this = this;
        wx.showModal({
            title: '提示',
            content: '确定要解绑该设备吗',
            complete: (res) => {
                if (res.confirm) {
                    wx.$http({
                        load: true,
                        method: 'get',
                        url: `/business/device/un-bind/${codes[index].id}`,
                        data: {}
                    }).then((res) => {
                        if(res.code == 1){
                            wx.showToast({
                                title: '解绑成功！',
                            })
                            setTimeout(()=>{
                                this.getUserInfo();
                            },1000)
                        }
                    })
                }
            }
        })
    },
    //链接
    test: function(e) {
        let {codes} = this.data;
        let index = e.currentTarget.dataset.index;
        let obj = codes[index];
        if(!obj.key){
            wx.showToast({
                title: '请输入设备绑定码！',
                icon: 'none'
            })
            return;
        }
        if(!obj.sn){
            wx.showToast({
                title: '请输入设备编号！',
                icon: 'none'
            })
            return;
        }
        wx.$http({
            load: true,
            url: '/business/device/bind',
            data: {
                key: obj.key,
                sn: obj.sn
            }
        }).then((res) => {
            if(res.code == 1){
                wx.showToast({
                    title: '绑定成功！',
                })
                setTimeout(()=>{
                    this.getUserInfo();
                },1000)
            }
        })
    },
    //返回
    goBack: function() {
        wx.$utils.goBack();
    }
})